package com.boe.dao.impl;

import com.boe.dao.UserDao;
import com.boe.domain.User;
import com.boe.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {

    @Override
    public User login(User user) {

        Connection conn=null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            //获得连接
            conn = JDBCUtils.getConnection();
            //编写SQL
            String sql = "select * from  user where username = ? and password = ?";
            //预编译SQL
            pstmt = conn.prepareStatement(sql);
            //填写参数
            pstmt.setString(1,user.getUsername());
            pstmt.setString(2,user.getPassword());
            //执行
            rs = pstmt.executeQuery();
            if(rs.next()) {
                User exitUser = new User();
                exitUser.setId(rs.getInt("uid"));
                exitUser.setUsername(rs.getString("username"));
                exitUser.setPassword(rs.getString("password"));

                return exitUser;
            }

        }catch (Exception e){
            e.printStackTrace();

        }finally {
            //释放资源
            JDBCUtils.release(rs,pstmt,conn);
        }
        return null;
    }
}
